package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.vioao.wechat.api.component.wxa.DomainApi;
import com.github.vioao.wechat.bean.entity.component.wxa.Domain;
import com.github.vioao.wechat.bean.response.BaseResponse;
import com.github.vioao.wechat.bean.response.component.wxa.ModifyDomainResponse;
import com.jmp.autocofiguration.WxConfigProperties;
import com.jmp.base.dto.WechatAuthorizerTokenRequest;
import com.jmp.base.dto.WechatAuthorizerTokenResponse;
import com.jmp.feign.WechatFeign;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.ResultModel;
import one.stand.service.DomainService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/5/13 14:25
 */
@Service
@Slf4j
public class DomainServiceImpl implements DomainService {
    @Autowired
    private WechatFeign wechatFeign;
    @Autowired
    private WxConfigProperties wxConfigProperties;

    @Override
    public ModifyDomainResponse modifyDomain(String appId) {
        ResultModel<WechatAuthorizerTokenResponse> authorizerTokenResponseResultModel = wechatFeign.authorizerToken(WechatAuthorizerTokenRequest.builder().appId(appId).build());
        AssertUtil.isTrue(authorizerTokenResponseResultModel);
        String token = authorizerTokenResponseResultModel.getData().getToken();
        List<String> reqList = new ArrayList<>();
        reqList.add("https://api.yiliit.com");
        reqList.add("https://restapi.amap.com");
        List<String> wss = new ArrayList<>();
        wss.add("wss://socket.yiliit.com");
        List<String> file = new ArrayList<>();
        file.add("https://api.yiliit.com");
        List<String> downloadFile = new ArrayList<>();
        downloadFile.add("https://one-stand.oss-cn-shanghai.aliyuncs.com");
        DomainApi.modifyDomain(token, JSON.toJSONString(Domain.builder().action("delete").build()));
        Domain domain = Domain.builder().action("add").requestdomain(reqList).uploaddomain(file).downloaddomain(downloadFile).wsrequestdomain(wss).build();
        return DomainApi.modifyDomain(token, JSON.toJSONString(domain));
    }

    @Override
    public ModifyDomainResponse getDomain(String appId) {
        ResultModel<WechatAuthorizerTokenResponse> authorizerTokenResponseResultModel = wechatFeign.authorizerToken(WechatAuthorizerTokenRequest.builder().appId(appId).build());
        AssertUtil.isTrue(authorizerTokenResponseResultModel);
        String token = authorizerTokenResponseResultModel.getData().getToken();
        return DomainApi.getDomain(token);
    }

    @Override
    public BaseResponse setWebViewDomain(String appId, String urls) {
        ResultModel<WechatAuthorizerTokenResponse> authorizerTokenResponseResultModel = wechatFeign.authorizerToken(WechatAuthorizerTokenRequest.builder().appId(appId).build());
        AssertUtil.isTrue(authorizerTokenResponseResultModel);
        String token = authorizerTokenResponseResultModel.getData().getToken();
//        if (StringUtils.isNoneBlank(urls)) {
//            log.info("#urls={}", urls);
//            List<String> list = Lists.newArrayList(urls.split(","));
//            return DomainApi.setWebViewDomain(token, "set", list);
//        } else {
//            return DomainApi.setWebViewDomain(token, "set", wxConfigProperties.getWhiteUrl());
//        }

        List<String> list = new ArrayList<>();
        list.add("https://cloud2.3dnest.cn");
        list.add("https://gzh.hongmushichang.com");
        list.add("https://720.hongmushichang.com");
        return DomainApi.setWebViewDomain(token, "set", list);
    }
}
